package com.iot.ic.controller;

import com.iot.ic.base.BaseController;
import com.iot.ic.common.Response;
import com.iot.ic.common.ReturnCode;
import com.iot.ic.model.RefereeInfo;
import com.iot.ic.model.StadiumResult;
import com.iot.ic.model.TeamInfo;
import com.iot.ic.service.LogService;
import com.iot.ic.service.RefereeService;
import com.iot.ic.service.StadiumResultService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SendToUser;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Api(description = "赛场成绩查询相关接口")
@RestController
public class StadiumResultController extends BaseController {
	
	@Autowired
	private StadiumResultService stadiumResultService;
	
	@Autowired
	private RefereeService refereeService;
	@Autowired
	private LogService logService;

	@Autowired
	SimpMessagingTemplate simpMessagingTemplate;

    /**
	 * 获得比赛信息接口
	 * @param userId
	 * @param stadiumResultId
	 * @return
     */
	@ApiOperation(value="获得比赛信息接口", notes="")
	@ResponseBody
	@RequestMapping(value="/users/{userId}/stadiumResult/{stadiumResultId}", method=RequestMethod.GET)
	public Response getStadiumResultById(@PathVariable("userId") int userId,
			@PathVariable("stadiumResultId") int stadiumResultId){
		Response response = new Response();
		StadiumResult stadiumResult = stadiumResultService.getStadiumResult(stadiumResultId);
		response.setResult(stadiumResult);
		return response;
	}

	/**
	 * 开始比赛相关接口
	 * @param userId 用户ID
	 * @param stadiumResult 比赛结果对象
     * @return
     */
	@ApiOperation(value="开始比赛相关接口", notes="")
	@ResponseBody
	@RequestMapping(value="/users/{userId}/match/start", method=RequestMethod.POST)
	public Response addStadiumResult(@PathVariable("userId") int userId,
									 @Valid@RequestBody StadiumResult stadiumResult){
		Response response = new Response();
		RefereeInfo user = refereeService.getRefereeInfo(userId);
		logService.addLogs(user.getRefereeNo(), user.getRefereeName(), new Date(), "开始比赛", "开始"+stadiumResult.getTeamName()+"车队在"+stadiumResult.getStadiumName()+"的比赛");
		int result = stadiumResultService.startRacing(stadiumResult);
		if (result > 0) {
			response.setResult(result);
            response.setInfos(ReturnCode.No_AddStadiumResultOK);
        } else {
            response.setInfos(ReturnCode.Err_AddStadiumResultFailor);
        }
		return response;
	}

	@ApiOperation(value="获得比赛成绩列表接口", notes="")
	@GetMapping(value = "/stadiuResults")
	public Response getFinishedResult(){
		return  null;
	}
	
	

	/**
	 * 结束比赛相关接口
	 * @param RefereeInfo
	 * @return
	 */
	@ApiOperation(value="结束比赛相关接口", notes="")
	@ResponseBody
	@RequestMapping(value="/users/{userId}/stadiumResult/{stadiumResultId}/match/end", method=RequestMethod.PUT)
	public Response endStadiumResult(
			@PathVariable("userId") int userId,
			@PathVariable("stadiumResultId") int stadiumResultId){
		Response response = new Response();
		StadiumResult stadiumResult = new StadiumResult();
		StadiumResult oldStadiumResult  = stadiumResultService.getStadiumResult(stadiumResultId);
		RefereeInfo user = refereeService.getRefereeInfo(userId);
		logService.addLogs(user.getRefereeNo(), user.getRefereeName(), new Date(), "结束比赛", "结束"+oldStadiumResult.getTeamName()+"在"+oldStadiumResult.getStadiumName()+"比赛");
		stadiumResult.setStadiumResultId(stadiumResultId);
		int result = stadiumResultService.endRacing(stadiumResult);
		if (result > 0) {
            response.setInfos(ReturnCode.No_EndMatchOK);
        } else {
            response.setInfos(ReturnCode.Err_EndMatchFailor);
        }
		return response;
	}
	
	/**
	 * 获得赛场比赛成绩详情
	 * @param RefereeInfo
	 * @return
	 */
	@ApiOperation(value="获得赛场比赛成绩详情", notes="")
	@ResponseBody
	@RequestMapping(value="/users/{userId}/stadiumResult/team/{teamId}", method=RequestMethod.GET)
	public Response getStadiumResult(@PathVariable("userId") int userId,
			@PathVariable("teamId") int teamId,@RequestParam(value = "scenesResultId",required = false) int scenesResultId){
		Response response = new Response();
		Map<String, Object> stadiumResultMap = stadiumResultService.getStadiumResultByTeam(teamId, scenesResultId);
		response.setResult(stadiumResultMap);
		return response;
	}

	@ApiOperation(value="获得某年赛场比赛成绩列表", notes="不带year这个参数的时候可以获取当年的成绩")
	@GetMapping("/users/{userId}/stadiumResults")
	public Response getStadiumResultList(@PathVariable("userId")int userId,@RequestParam(value = "year",required = false,defaultValue = "2017")int year){
		Response response=new Response();
		List<Map<String,Object>> list=stadiumResultService.getStadiumResultByYear(year);
		response.setResult(list);
		return  response;
	}

	@ApiOperation(value="获得某年赛场比赛成绩列表，websocket请求", notes="不带year这个参数的时候可以获取当年的成绩")
	@MessageMapping("/users/stadiumResultsSocket")
	@SendToUser("/users/results")
	public void getStadiumResultListSocket(
			@Header("user")String userId,
			@Header(value = "year",required = false,defaultValue = "2017")int year)
	{
		Response response=new Response();
		List<Map<String,Object>> list=stadiumResultService.getStadiumResultByYear(year);
		response.setResult(list);

		simpMessagingTemplate.convertAndSendToUser(userId, "/users/results", response);
	}

	@ApiOperation(value="获得某年赛场详细比赛成绩列表", notes="不带year这个参数的时候可以获取当年的成绩")
	@GetMapping("/users/{userId}/stadiumResultDetails")
	public Response getStadiumResultDetailList(@PathVariable("userId")int userId,@RequestParam(value = "year",required = false,defaultValue = "2017")int year){
		Response response=new Response();
		List<Map<String,Object>> list=stadiumResultService.getStadiumResultDetailByYear(year);
		response.setResult(list);
		return  response;
	}
	
	
	@ApiOperation(value="根据赛场获得每个赛场的比赛成绩榜")
	@GetMapping("/users/{userId}/stadiumType/{stadiumType}/stadiumResultDetails")
	public Response listStadiumResultDetailByStadiumType(@PathVariable("userId")int userId,@PathVariable("stadiumType") int stadiumType){
		Response response=new Response();
		List<Map<String,Object>> list=stadiumResultService.getStadiumResultDetailByStadiumType(stadiumType);
		response.setResult(list);
		return  response;
	}


	@ApiOperation(value="获得赛场比赛成绩详情,包含场景得分列表", notes="")
	@GetMapping(value="/users/{userId}/teams/{teamId}/stadiumResults")
	public Response getStadiumScoreDetail(@PathVariable("userId")int userId,@PathVariable("teamId")int teamId){
		Response response=new Response();
		List<StadiumResult>list=stadiumResultService.getStadiumResultWithScenesResult(teamId);
		response.setResult(list);
		return response;
	}
	
	@ApiOperation(value="根据车队和赛场获得赛场比赛成绩详情,包含场景得分列表", notes="")
	@GetMapping(value="/users/{userId}/teams/{teamId}/stadiumResult")
	public Response getStadiumScoreDetailByStadiumAndTeam(@PathVariable("userId")int userId,@PathVariable("teamId")int teamId,
			@RequestParam(value = "stadiumId",required = false) int stadiumId){
		Response response=new Response();
		StadiumResult result =stadiumResultService.getStadiumResultWithScenesResult(teamId, stadiumId);
		response.setResult(result);
		return response;
	}
	
	@ApiOperation(value="修改赛场成绩信息", notes="")
	@ResponseBody
	@RequestMapping(value="/users/{userId}/stadiumResult/{stadiumResultId}", method=RequestMethod.PUT)
	public Response updateStadiumResult(@PathVariable("userId")int userId,
			@PathVariable("stadiumResultId")int stadiumResultId,@RequestBody @Valid StadiumResult stadiumResult){
		Response response=new Response();
		stadiumResult.setStadiumResultId(stadiumResultId);
		int result = stadiumResultService.updateStadiumResult(stadiumResult);
		 if (result==1)
	         response.setInfos(ReturnCode.No_UpdateOKStadiumResultOK);
	     else
	         response.setInfos(ReturnCode.Err_UpdateOKStadiumResultFailor);
		return response;
	}

}
